<template>
  <el-card class="applyCard">
    <div style="margin-left: 40px;font-size:36px">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="申请人" v-model="form.applicant">
          <span>{{ form.applicant }}</span>
        </el-form-item>
        <el-form-item label="联系方式" v-model="email">
          <span>{{ email }}</span>
        </el-form-item>
        <el-form-item label="所需硬件">
          <el-select v-model="form.hardware" filterable>
            <el-option v-for="item in hardwareList" :key="item.id " :label="item.name" :value="item.name">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.description }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用途">
          <el-input v-model="form.purpose" type="textarea" clearable style="width: 30%" show-word-limit maxlength="100"
                    :row="3"
                    placeholder="请输入用途"></el-input>
        </el-form-item>
        <el-form-item label="申请时间">
          <el-date-picker v-model="datePick"
                          type="daterange"
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"
                          @change="handleDatePick"></el-date-picker>
        </el-form-item>
        <el-form-item>

            <el-button type="primary" @click="onSubmit">提交申请</el-button>
            <el-button @click="resetForm" style="margin-left: 60px">重置</el-button>

        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>
<script>
export default {
  name: "Apply",
  data() {
    return {
      form: {
        hardware: "",
        purpose: "",
        startTime: "",
        endTime: "",
        applicant: "",
      },
      username:"",
      email:"",
      datePick: [],
      hardwareList: [],
    }
  },
  created() {
    let user = JSON.parse(localStorage.getItem('user'))
    this.form.applicant = user.username
    this.email = user.email
    this.getHardwareList()
  },
  methods: {
    async getHardwareList() {
      // 调用post请求
      const {data: res} = await this.$http.get("getAllHardware");
      this.hardwareList = res.hardwareList; // 将返回数据赋值
    },
    handleDatePick() {
      console.log(this.datePick)
      this.form.startTime = this.datePick[0];
      this.form.endTime = this.datePick[1];
    },
    async onSubmit() {
      console.log(this.form)
      const {data: res} = await this.$http.post("addApplication", this.form)
      if (res.msg === 'success') this.$message.success(res.msg)
      else this.$message.error(res.msg)
    },
    resetForm() {
      this.$refs.form.resetFields()
    }

  }
}
</script>

<style scoped>
.applyCard {
  width: 50%;
  margin: 2vh 4vh;
}
</style>